//
//  ViewController.swift
//  HZJGTEST
//
//  Created by 运车管家 on 2017/8/22.
//  Copyright © 2017年 运车管家. All rights reserved.
//

import UIKit
import CoreData

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
//        test1()
        ys()
    }
    
    func ys() {
        let imgV = UIImageView(frame: self.view.frame)
        self.view.addSubview(imgV)
        imgV.image = UIImage(named: "Default")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//        let model = HZJModel()
//        print(model)
//        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
//        print(path)
//        test2()
        
        
        let vc = HZJMainViewController()
        self.navigationController?.pushViewController(vc, animated: true);
    }

    
    //增
    func test1() {
        let model = HZJModel()
        model.crit = 100
        
        let deleagte = UIApplication.shared.delegate as! AppDelegate
        let context = deleagte.persistentContainer.viewContext
        
        let people = NSEntityDescription.insertNewObject(forEntityName: "HZJModel", into: context) as! HZJModel
        people.type = 2
        people.crit = 10
        people.hit = 12
        
        let entity = NSEntityDescription.entity(forEntityName: "HZJModel", in: context)
        
        let theModel = NSManagedObject(entity: entity!, insertInto: context)
        
        theModel.setValue(model.type, forKey: "type")
        theModel.setValue(model.crit, forKey: "crit")
        theModel.setValue(model.hit, forKey: "hit")
        theModel.setValue(model.vitalRates, forKey: "vitalRates")
        
        do {
            try context.save()
            print("saved")
        }catch{
            print(error)
        }
        
    }
    //查
    func test2(){
        
        let deleagte = UIApplication.shared.delegate as! AppDelegate
        let context = deleagte.persistentContainer.viewContext
        
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "HZJModel")
        fetchRequest.predicate = NSPredicate(format: "type=1") //设置条件
        do {
            let searchResults = try context.fetch(fetchRequest)
            print("numbers of \(searchResults.count)")
            
            for p in (searchResults as! [NSManagedObject]){
                print("type:  \(p.value(forKey: "type")!) crit: \(p.value(forKey: "crit")!) hit:  \(p.value(forKey: "hit")!) vitalRates: \(p.value(forKey: "vitalRates")!)")
            }
        } catch  {
            print(error)
        }
    }
//    删
    func test3(){
        let deleagte = UIApplication.shared.delegate as! AppDelegate
        let context = deleagte.persistentContainer.viewContext
        
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "HZJModel")
        
        do {
            let searchResults = try context.fetch(fetchRequest) as! [HZJModelManagedObject]
            print("numbers of \(searchResults.count)")
            for p in searchResults{
                print("type:  \(p.type) crit: \(p.crit) hit:  \(p.hit)!) vitalRates: \(p.vitalRates)")
                context.delete(p)
            }
        } catch  {
            print(error)
        }
    }
}

